package com.zhixiang.core.command;


import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

@SuppressWarnings({"unchecked"})
public class FieldCommandImpl
  implements CriteriaCommand
{
  private String property;
  private Object value;
  private String operation;
  private QueryFilter filter;

  public FieldCommandImpl(String s, Object obj, String s1, QueryFilter queryfilter)
  {
    this.property = s;
    this.value = obj;
    this.operation = s1;
    this.filter = queryfilter;
  }

  public String getProperty() {
    return this.property;
  }

  public void setProperty(String s) {
    this.property = s;
  }

  public Object getValue() {
    return this.value;
  }

  public void setValue(Object obj) {
    this.value = obj;
  }

  public String getOperation() {
    return this.operation;
  }

  public void setOperation(String s) {
    this.operation = s;
  }

  public Criteria execute(Criteria c) {
	  Criteria criteria=c;
	String sproperty=this.property;
    String[] as = this.property.split("[.]");
    if ((as != null) && (as.length > 1) && (!"vo".equals(as[0]))) {    	
      for (int i = 0; i < as.length - 1; i++) {
        if (this.filter.getAliasSet().get(as[i])==null) {
        	//criteria=criteria.createAlias(as[i], as[i]);
        	criteria=criteria.createCriteria(as[i], as[i]);
          this.filter.getAliasSet().put(as[i],criteria);
        } else{
        	criteria=(Criteria)filter.getAliasSet().get(as[i]);
        }
      }
      sproperty=as[as.length-1];
    }
    if ("LT".equals(this.operation))
      criteria.add(Restrictions.lt(sproperty, this.value));
    else if ("GT".equals(this.operation))
      criteria.add(Restrictions.gt(sproperty, this.value));
    else if ("LE".equals(this.operation))
      criteria.add(Restrictions.le(sproperty, this.value));
    else if ("GE".equals(this.operation))
      criteria.add(Restrictions.ge(sproperty, this.value));
    else if ("LK".equals(this.operation))
      criteria.add(Restrictions.like(
    		  sproperty, 
        "%" + this.value + "%")
        .ignoreCase());
    else if ("LFK".equals(this.operation))
      criteria.add(Restrictions.like(sproperty, 
        this.value + "%")
        .ignoreCase());
    else if ("RHK".equals(this.operation))
      criteria.add(Restrictions.like(sproperty, 
        "%" + this.value)
        .ignoreCase());
    else if ("NULL".equals(this.operation))
      criteria.add(Restrictions.isNull(sproperty));
    else if ("NOTNULL".equals(this.operation))
      criteria.add(Restrictions.isNotNull(sproperty));
    else if ("EMP".equals(this.operation))
      criteria.add(Restrictions.isEmpty(sproperty));
    else if ("NOTEMP".equals(this.operation))
      criteria.add(Restrictions.isNotEmpty(sproperty));
    else if ("NEQ".equals(this.operation))
      criteria.add(Restrictions.ne(sproperty, this.value));
    else
      criteria.add(Restrictions.eq(sproperty, this.value));
    return c;
  }

  public String getPartHql() {
    String[] as = this.property.split("[.]");
    if ((as != null) && (as.length > 1) && (!"vo".equals(as[0])) && 
      (this.filter.getAliasSet().get(as[0])==null))
      this.filter.getAliasSet().put(as[0],"s");
    String s = "";
    if ("LT".equals(this.operation)) {
      s = this.property + " < ? ";

      this.filter.getParamValueList().add(this.value);
    } else if ("GT".equals(this.operation)) {
      s = this.property + " > ? ";

      this.filter.getParamValueList().add(this.value);
    } else if ("LE".equals(this.operation)) {
      s = this.property + " <= ? ";

      this.filter.getParamValueList().add(this.value);
    } else if ("GE".equals(this.operation)) {
      s = this.property + " >= ? ";

      this.filter.getParamValueList().add(this.value);
    } else if ("LK".equals(this.operation)) {
      s = this.property + " like ? ";

      this.filter.getParamValueList().add(
        "%" + this.value.toString() + 
        "%");
    } else if ("LFK".equals(this.operation)) {
      s = this.property + " like ? ";

      this.filter.getParamValueList().add(
        this.value.toString() + "%");
    }
    else if ("RHK".equals(this.operation)) {
      s = this.property + " like ? ";

      this.filter.getParamValueList().add(
        "%" + this.value.toString());
    }
    else if ("NULL".equals(this.operation)) {
      s = this.property + " is null ";
    }
    else if ("NOTNULL".equals(this.operation)) {
      s = this.property + " is not null ";
    }
    else if ((!"EMP".equals(this.operation)) && (!"NOTEMP".equals(this.operation))) {
      if ("NEQ".equals(this.operation)) {
        s = this.property + " !=? ";

        this.filter.getParamValueList().add(this.value);
      } else {
        s = s + this.property + 
          " =? ";
        this.filter.getParamValueList().add(this.value);
      }
    }
    return s;
  }
}

